package cn.lcsjsxy.yxc.ioandnetwork;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * UDP协议的网络编程
 */
public class UDPTest {
    //发送端
    @Test
    public void send(){
        DatagramSocket socket=null;
        InetAddress address=null;
        DatagramPacket packet=null;
        try {
            socket=new DatagramSocket();

            String str="我是UDP方式发送的导弹";
            byte[] data=str.getBytes();
            address=InetAddress.getLocalHost();
            packet=new DatagramPacket(data,0,data.length,address,9090);
            socket.send(packet);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (socket!=null)
                socket.close();
        }
    }

    //接收端
    @Test
    public void receiver(){

        DatagramSocket socket= null;
        try {
            socket = new DatagramSocket(9090);

            byte[] buffer = new byte[100];

            DatagramPacket packet=new DatagramPacket(buffer,0,buffer.length);

            socket.receive(packet);

            System.out.println(new String(packet.getData(),0, packet.getLength()));
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (socket!=null)
                socket.close();
        }


    }
}
